package com.xiaofeixia.sj.controller;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xiaofeixia.common.AppKey;
import com.xiaofeixia.common.RespCode;
import com.xiaofeixia.common.RespEntity;
import com.xiaofeixia.sj.entity.User;
import com.xiaofeixia.sj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/api/User")
public class UserController {
    /**
     * 调用加密后的API唯一标识
     */
    @Autowired()
    @Qualifier("AppKey")
    private AppKey appKey;


    /**
     * 调用userservice
     */
    @Autowired
    @Qualifier("userService")
    private UserService userService;

    /**
     * 用户信息
     * @param
     * @return
     */
    @ResponseBody //返回json*/
    @RequestMapping("/User")
    public RespEntity login() {    //返回数据为状态实体类
        List<User> user = userService.SelectUser();
        if (user!=null) {
            return new RespEntity(RespCode.SUCCESS, user);
        }
            return new RespEntity(RespCode.WARN, user);
    }


    /**
     * 登陆
     * @param username
     * @param password
     * @return
     */
    @ResponseBody
    @PostMapping("/UserLogin/username={username}&password={password}&appkey={appkey}")
    public RespEntity Login(@PathVariable("username")String username,@PathVariable("password")String password
    ,@PathVariable("appkey")String appkey){
        String status=null;
        User user = userService.SelectLogin(username,password);
        Boolean appkeys  = appKey.GetAppKey(appkey);
        //验证调的appID
        if(appkeys){
            System.out.println("调用方验证成功");
            //随机数最小100000  最大999999
            /*Integer suijishu = RandomUtil.randomInt(100000,999999);
            Console.log(suijishu);
            http抓取网页
            String http = HttpUtil.get("https://www.baidu.com");
            Console.log(http);*//*
            //MD5工具类
            String a = SecureUtil.md5("123");
            System.out.println("md5--->"+a);
            //验证身份证
            String shenfenzheng="371322199703013415";
            //身份证是是否合法
            Boolean id = IdcardUtil.isValidCard(shenfenzheng);
            Console.log("身份证是否合法-->"+id);

            //获取生日
            String br = IdcardUtil.getBirthByIdCard(shenfenzheng);
            Console.log("身份证是否合法-->"+br);
            //获取年龄
            Integer age = IdcardUtil.getAgeByIdCard(shenfenzheng);
            Console.log("获取年龄"+age);
            //获取性别 1男 2女
            Integer byIdCard = IdcardUtil.getGenderByIdCard(shenfenzheng);
            Console.log("性别----->"+byIdCard);
            String shen = IdcardUtil.getProvinceByIdCard(shenfenzheng);
            Console.log("省份---->    "+shen);
            //MailUtil.send("sngjie_abc@163.com","测试","邮件来自Java",false);
            //验证登陆
            JSONObject json = JSONUtil.createObj();
            json.put("a","b");
            json.put("b","b");
            json.put("c","b");
            json.put("d","b");
            json.put("e","b");
            String jsonstr = JSONUtil.toJsonStr(json);
            System.out.println("jsonstr----->"+jsonstr);
            //制作二维码
            QrCodeUtil.generate("http://47.98.203.160/maven_ssm/",500,500, FileUtil.file("F:\\idea\\erweima.jpg"));
            //解密二维码
            String coo = QrCodeUtil.decode(FileUtil.file("F:\\idea\\erweima.jpg"));
            System.out.println("coo----->"+coo);
            //post请求
            String post = HttpUtil.post("http://47.98.203.160/maven_ssm/","456456");
            System.out.println("post---->"+post);*/
            return new RespEntity(RespCode.SUCCESS,user);  //返回null为登录失败

        }else {
            System.out.println("调用方验证失败");
            System.out.println("调用方验证失败");
            status  = "-1";
            return new RespEntity(RespCode.APPID,status);  //返回-1是验证APPID失败
        }
    }


    /**
     * 注册(接收json实体类)
     * @return
     */
    @ResponseBody
    @PostMapping("/UserRegistered")
    public RespEntity UserRegistered(){
        String status=null;
        //POST请求
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("city", "北京");

        String result1 = HttpUtil.post("https://www.baidu.com", paramMap);
        Console.log("result1---->"+result1);
        return new RespEntity(RespCode.SUCCESS,status=null);
    }
}
